package com.user.controllers;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.user.bean.Page;
import com.user.bean.User;
import com.user.service.UserService;

/**
 * 用户类控制器
 * @author LIU
 * @version 2.0 2017-10-30
 */
@Controller
public class UserController {

	ModelAndView mav = null;
	
	@Autowired
	UserService userService;
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	/**
	 * 用户登陆
	 *TODO
	 *LIU
	 * @param request
	 * @param response
	 * @return ModelAndView
	 *下午2:50:51
	 */
	@RequestMapping("/login.action")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
		
		// 获取账户名 和 密码object
  		String uname = (String)request.getParameter("uname");
  		String password = (String)request.getParameter("pwd");
  		
  		// 将用户名和密码保存到user
  		User user = new User();
  		user.setUserId(uname);
  		user.setPassword(password);
  		
  		
  		// 判断登陆是否成功
  		if(userService.login(user)){
//  		Cookie c1 = new Cookie("uname",uname);
//			Cookie c2 = new Cookie("password",password);
//			c1.setMaxAge(100);
//			c2.setMaxAge(100);
//			response.addCookie(c1);
//			response.addCookie(c2);
  			mav = new ModelAndView("showAll.action");
			
  		}else{
  			request.getSession().setAttribute("mess", "登陆失败！");
  			mav = new ModelAndView("redirect:login.jsp");
  		}
  		
		return mav;
	}
	
	
	/**
	 * 用户登陆
	 *TODO
	 *LIU
	 * @param request
	 * @param response
	 * @return ModelAndView
	 *下午2:50:51
	 */
	@RequestMapping("/login1.action")
	@ResponseBody
	public Object login1(HttpServletRequest request, HttpServletResponse response) {
		
		// 将用户名和密码保存到user
		
		User user = new User();
		user.setUserId(request.getParameter("uname"));
		user.setPassword(request.getParameter("pwd"));
		
		String flag = "false";
		// 判断登陆是否成功
		if(userService.login(user)){
			flag = "true";
			
		}else{
			flag = "false";
		}
		
		return flag;
	}
	
	
	/**
	 * 分页显示全部
	 *TODO
	 *LIU
	 * @param request
	 * @param response
	 * @return
	 *下午4:16:25
	 */
	@RequestMapping("showAll.action")
	public ModelAndView showAll(HttpServletRequest request, HttpServletResponse response) {
		
		// 初始化分页信息
		Page p = new Page();
		p.setCurrCount(3); // 每页显示记录数
		p.setTotalCount(userService.searchAllCount()); // 表中的总记录数
		if(p.getTotalCount() % p.getCurrCount() == 0){ // 设置表的总页数
			p.setSumPage(p.getTotalCount() / p.getCurrCount());
		}else{
			p.setSumPage(p.getTotalCount() / p.getCurrCount());
		}
		
		if(request.getParameter("curr") == null){// 默认显示第一页
			p.setCurrPage(1);
			List<User> userList = userService.getUserByPage(p);
			mav = new ModelAndView("showAll.jsp");
			mav.addObject("userList", userList);
			mav.addObject("page", p);
		}else{// Ajax实现分页
			p.setCurrPage(Integer.valueOf(request.getParameter("curr")));
			List<User> userList = userService.getUserByPage(p);
			mav = new ModelAndView("divShowAll.jsp");
			mav.addObject("userList", userList);
			mav.addObject("page", p);
		}
		
		return mav;
	}
	
	
	
	/**
	 * 查询用户信息
	 *TODO
	 *LIU
	 * @param request
	 * @param response
	 * @return
	 *下午4:25:20
	 */
	@RequestMapping("search.action")
	public ModelAndView search(HttpServletRequest request, HttpServletResponse response){
		
		mav = new ModelAndView("search.jsp");
		String userId = request.getParameter("searchId");
		User user = userService.searchUser(userId);
		
		if(user != null){
			mav = new ModelAndView("search.jsp");
			mav.addObject("user", user);
		}else{
			mav = new ModelAndView("showAll.jsp");
		}
		
		return mav;
	}
	
	
	
	/**
	 * 修改用户信息
	 *TODO
	 *LIU
	 * @param request
	 * @param response
	 * @return
	 *下午4:25:20
	 */
	@RequestMapping("update.action")
	public ModelAndView update(HttpServletRequest request, HttpServletResponse response){
		
		User user = new User();
		user.setUserId(request.getParameter("uname"));
		user.setUserName(request.getParameter("realName"));
		user.setPassword(request.getParameter("pwd"));
		user.setSex(request.getParameter("sex"));
		user.setAge(Integer.parseInt(request.getParameter("age")));
		user.setIdcard(request.getParameter("cardID"));
		user.setPhone(request.getParameter("phone"));
		user.setAddress(request.getParameter("address"));
		user.setEmail(request.getParameter("email"));
		
		userService.updateUser(user, user.getUserId());
		
		mav = new ModelAndView("showAll.jsp");
		
		return mav;
	}
	
	
	
	
	/**
	 * 注册用户信息
	 *TODO
	 *LIU
	 * @param request
	 * @param response
	 * @return
	 *下午4:25:20
	 */
	@RequestMapping("register.action")
	public ModelAndView register(HttpServletRequest request, HttpServletResponse response){
		
		User user = new User();
		user.setUserId(request.getParameter("uname"));
		user.setUserName(request.getParameter("realName"));
		user.setPassword(request.getParameter("pwd"));
		user.setSex(request.getParameter("sex"));
		user.setAge(Integer.parseInt(request.getParameter("age")));
		user.setIdcard(request.getParameter("cardID"));
		user.setPhone(request.getParameter("phone"));
		user.setAddress(request.getParameter("address"));
		user.setEmail(request.getParameter("email"));
		
		int result = userService.addUser(user);
		
		if(result > 0){
			mav = new ModelAndView("login.jsp");
			request.setAttribute("mess", "注册成功！");
		}else{
			request.getSession().setAttribute("mess", "注册失败！");
			mav = new ModelAndView("redirect:register.jsp");
		}
		
		return mav;
	}
	
	
	
	
	/**
	 * 删除用户功能
	 *TODO
	 *LIU
	 * @param request
	 * @param response
	 * @return
	 *下午4:56:19
	 */
	@RequestMapping("update.action")
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) {
		
		String userId = request.getParameter("deleteId");
		
		int result = userService.deleteUser(userId);
		
		PrintWriter out = null;
		try {
			out = response.getWriter();
		
			if(result > 0){
				out.print("<script type='text/javascript'>alert('删除成功！');window.location='showAll.jsp';</script>");
			}else{
				out.print("<script type='text/javascript'>alert('删除失败！');window.location='showAll.jsp';</script>");
			}
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return mav;
	}
	
	
	
	
	/**
	 * 文件上传
	 *TODO
	 *LIU
	 * @param request
	 * @param response
	 * @return
	 *上午10:34:15
	 */
	@RequestMapping("uploadFile.action")
	public ModelAndView uploadFile(@RequestParam("uploadfile") CommonsMultipartFile cmf, HttpServletRequest request) throws Exception{
		
		// 接收普通的用户名的话，用参数request来接收
		String uname = request.getParameter("uname");
		String path = "F:\\upload\\" + cmf.getOriginalFilename();
		File file = new File(path);
		
		cmf.transferTo(file);
		
		mav = new ModelAndView("uploadResult.jsp");
		String mess = cmf.getOriginalFilename() + "上传成功了！";
		mav.addObject("mess", mess);
		mav.addObject("uname", uname);
		return mav;
	}
	
	
	
	
	
}
